package com.controller;

import com.entry.OrderForm;
import com.entry.Result;
import com.server.OrderFormService;
import com.server.SellService;
import com.util.RedisUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    RedisUtil redisUtil;
    @Resource
    OrderFormService orderFormService;
    @Resource
    SellService sellService;


    @RequestMapping("/order")
    public String home(@Param("status") int status, HttpSession session){
        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));
        if (redisUtil.get("member_id")!=null){
            int user_id=member_id;
            List<OrderForm> orderFormList=orderFormService.getOrderSellandStatus(user_id, status);
            orderFormList.forEach(System.out::println);
            session.setAttribute("orderFormList",orderFormList);
        }


        return "order";
    }


    //更新商品状态
    @RequestMapping(value = "/order/updateStatus",method = RequestMethod.GET)
    @ResponseBody
    public Result updateStatus(@Param("order_code") String order_code,@Param("status") int status, HttpSession session){
        System.out.println("order_code="+order_code);
        System.out.println("status="+status);
        //当前时间日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Date d = new Date(System.currentTimeMillis());
        Result result = new Result();
        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));
        if (redisUtil.get("member_id")!=null){
            //int count = orderFormService.updateOrderStatus(status, order_code);
            int count = orderFormService.modifyOrderStatus_confirm_date(status, d, order_code);
            if (count>0){
                result.setSuccess(true);
                return result;
            }
        }
        result.setSuccess(false);
        return result;
    }


    //删除未支付的商品订单
    @RequestMapping(value = "/order/deleteOrderForm",method = RequestMethod.GET)
    @ResponseBody
    public Result updateStatus(@Param("order_code") String order_code,HttpSession session){
        System.out.println("order_code="+order_code);
        Result result = new Result();
        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));
        if (redisUtil.get("member_id")!=null){
            int count = orderFormService.deleteOrderForm(order_code);
            int count1 = sellService.deleteSell(order_code);
            if (count>0){
                result.setSuccess(true);
                return result;
            }
        }
        result.setSuccess(false);
        return result;
    }

}
